package com.changy.ai_tutor_for_dm.service.impl;

import com.changy.ai_tutor_for_dm.entity.*;
import com.changy.ai_tutor_for_dm.mapper.*;
import com.changy.ai_tutor_for_dm.service.QuestionsService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class QuestionsServiceImpl implements QuestionsService {

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private QuestionsMapper questionsMapper;

    @Autowired
    private QuestionsSingleMapper questionsSingleMapper;

    @Autowired
    private QuestionsMultipleMapper questionsMultipleMapper;

    @Autowired
    private QuestionsTrueFalseMapper questionsTrueFalseMapper;

    @Autowired
    private QuestionsFillMapper questionsFillMapper;

    @Autowired
    private QuestionTagsMapper questionTagsMapper;

    @Transactional
    @Override
    public void postQuestions(Map<String, Object> requestbody) throws  Exception{
        log.info("前端传来的对象: " + requestbody);

        // 1. 解析info字段，转为Questions对象
        Questions questions = objectMapper.convertValue(requestbody.get("info"), Questions.class);
        // 2. 给questions对象设置创建时间和更新时间
        questions.setCreateTime(LocalDateTime.now());
        questions.setUpdateTime(LocalDateTime.now());
        // 3. 保存Questions对象到数据库
        questionsMapper.insert(questions);
        // 4. 根据type 字段，调用对应的服务
        switch (questions.getType()) {
            case "single":
                // 1. 解析detail字段，转为QuestionsSingle对象
                QuestionsSingle questionsSingle = objectMapper.convertValue(requestbody.get("detail"), QuestionsSingle.class);
                // 2. 给QuestionsSingle对象设置questionId
                questionsSingle.setQuestionId(questions.getId());
                // 3. 给QuestionsSingle对象设置options字段，将optionlist转为JSON字符串
                questionsSingle.setOptions(objectMapper.writeValueAsString(questionsSingle.getOptionlist()));
                // 4. 保存QuestionsSingle对象到数据库
                questionsSingleMapper.insert(questionsSingle);
                // 5. 解析tags字段，转为List<Tag>对象
                List<Tag> tags1 = objectMapper.convertValue(requestbody.get("tags"),
                        objectMapper.getTypeFactory().constructCollectionType(List.class, Tag.class));
                // 6. 保存题目-标签关系对象到数据库
                Long questionId1 = questions.getId();
                for (Tag tag : tags1) {
                    Long tagId = tag.getId();
                    questionTagsMapper.insert(questionId1, tagId);
                }
                break;
            case "multiple":
                // 1. 解析detail字段，转为QuestionsMutiple对象
                QuestionsMultiple questionsMultiple = objectMapper.convertValue(requestbody.get("detail"), QuestionsMultiple.class);
                // 2. 给QuestionsMutiple对象设置questionId
                questionsMultiple.setQuestionId(questions.getId());
                // 3. 给QuestionsMutiple对象设置options字段，将optionlist转为JSON字符串
                questionsMultiple.setOptions(objectMapper.writeValueAsString(questionsMultiple.getOptionlist()));
                // 4. 给QuestionsMutiple对象设置correctOptions字段，将correctOptionlist转为JSON字符串
                questionsMultiple.setCorrectOptions(objectMapper.writeValueAsString(questionsMultiple.getCorrectOptionlist()));
                // 5. 保存QuestionsMutiple对象到数据库
                questionsMultipleMapper.insert(questionsMultiple);
                // 6. 解析tags字段，转为List<Tag>对象
                List<Tag> tags2 = objectMapper.convertValue(requestbody.get("tags"),
                        objectMapper.getTypeFactory().constructCollectionType(List.class, Tag.class));
                // 7. 保存题目-标签关系对象到数据库
                Long questionId2 = questions.getId();
                for (Tag tag : tags2) {
                    Long tagId = tag.getId();
                    questionTagsMapper.insert(questionId2, tagId);
                }
                break;
            case "true_false":
                // 1. 解析detail字段，转为QuestionsTrueFalse对象
                QuestionsTrueFalse questionsTrueFalse = objectMapper.convertValue(requestbody.get("detail"), QuestionsTrueFalse.class);
                // 2. 给QuestionsTrueFalse对象设置questionId
                questionsTrueFalse.setQuestionId(questions.getId());
                // 3. 保存QuestionsTrueFalse对象到数据库
                questionsTrueFalseMapper.insert(questionsTrueFalse);
                // 4. 解析tags字段，转为List<Tag>对象
                List<Tag> tags3 = objectMapper.convertValue(requestbody.get("tags"),
                        objectMapper.getTypeFactory().constructCollectionType(List.class, Tag.class));
                // 5. 保存题目-标签关系对象到数据库
                Long questionId3 = questions.getId();
                for (Tag tag : tags3) {
                    Long tagId = tag.getId();
                    questionTagsMapper.insert(questionId3, tagId);
                }
                break;
            case "fill":
                // 1. 解析detail字段，转为QuestionsFill对象
                QuestionsFill questionsFill = objectMapper.convertValue(requestbody.get("detail"), QuestionsFill.class);
                // 2. 给QuestionsFill对象设置questionId
                questionsFill.setQuestionId(questions.getId());
                // 3. 给QuestionsFill对象设置correctAnswers字段，将correctAnswerlist转为JSON字符串
                questionsFill.setCorrectAnswers(objectMapper.writeValueAsString(questionsFill.getCorrectAnswerlist()));
                // 4. 保存QuestionsFill对象到数据库
                questionsFillMapper.insert(questionsFill);
                // 5. 解析tags字段，转为List<Tag>对象
                List<Tag> tags4 = objectMapper.convertValue(requestbody.get("tags"),
                        objectMapper.getTypeFactory().constructCollectionType(List.class, Tag.class));
                // 6. 保存题目-标签关系对象到数据库
                Long questionId4 = questions.getId();
                for (Tag tag : tags4) {
                    Long tagId = tag.getId();
                    questionTagsMapper.insert(questionId4, tagId);
                }
                break;
            case "essay":
                // 调用问答题服务
                break;
            default:
                // 默认处理
                break;
        }

    }
}
